/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import com.edi.proxy.component.*;
	import com.edi.runtime.Event.EDIBasicDataEvent;
	import com.edi.runtime.control.*;
	import com.edi.utils.HashMap;
	
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import mx.collections.ArrayCollection;
	import mx.collections.XMLListCollection;
	import mx.containers.Box;
	import mx.containers.HBox;
	import mx.containers.HDividedBox;
	import mx.containers.VBox;
	import mx.containers.VDividedBox;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.CheckBox;
	import mx.controls.Label;
	import mx.controls.List;
	import mx.controls.TextInput;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.ClassFactory;
	import mx.events.FlexEvent;
	import mx.events.ListEvent;
	/**
	 * 基础资料弹出窗口组件
	 */
	public class EDIBasicDataWindow extends EDITitleWindow
	{
		public var itemClassName:String="";
		public var itemClassID:String;
		public var descriptXml:XML;
		public var resultXml:XML;
		public var treeOrderField:String;
		public var treeFliter:String;
		public var returnTxt:TextInput;
		
		private var MainBox:VBox;
		private var LeftBox:VDividedBox;
		private var RightBox:Box;
		private var ItemGrid:EDIDataGrid;
		private var ItemTree:EDITree;
		private var ItemList:List;
		//Build By HW
		/**
		 *左下角的父容器 
		 */		
		private var LeftBottomHbox:VBox; 
		/**
		 *清除和删除 按钮的容器 
		 */		
		private var BtnBox:HBox;  
		/**
		 *清除按钮 
		 */		
		var clearBtn:Button;  
		/**
		 *删除按钮 
		 */		
		var delBtn:Button;	
		private var HDivBox:HDividedBox;
		private var CheckAll:CheckBox;
		private var bottomBox:HBox;
		private var OkBtn:Button;
		private var ResultTxt:TextInput;
		private var hasShowAlert:Boolean = false;
		
		private var eql:String="";
		private var eqlHead:String="";
		private var _returnArr:ArrayCollection = new ArrayCollection();
		
		private var visibleColObj:Object=new Object();
		private var visibleColNum:int=0;
		
		private var itemID:String;			//基础资料表名
		private var treeItemID:String;		//树状基础资料表名
		private var treeRootValue:String;	//树状根节点上级id
		private var treeLabelField:String;	//树形显示节点
		private var treeIDField:String;		//树形id字段
		private var treepidField:String;	//树形上级字段
		private var itemParentID:String;	//基础资料和树形资料关联字段
		public var callback:Function;
		public var multiSelected:Boolean=false;
		public var displayField:String="caption";	//返回基础资料字段
		public var saveField:String="id";
		
		private var gridDesXml:XML;
		private var proxy:HttpProxy;
		public var ediBasicData:EDIBasicData;
		public var sessionID:String;
		
		 /**
		 *数据源名称
		 */
		 public var datasouName:String;
		 
		 /**
		 * 是否为逻辑删除(逻辑删除需要存在字段isdel来标示,0为未删除,1为删除.数据库中为bit类型,默认为物理删除,即直接删除数据库中的数据.)
		 */
		 public var logicDelete:Boolean = false;
		 /**
		 * 服务中需要传递的参数.默认长度为空.
		 */
		 public var paramMap:HashMap = new HashMap();
		/**
		 * 构造函数，构造可视化界面，并根据传入的参数向服务器索取相应的基础资料数据
		 * @param itemID 表格数据来源,可以是基础资料ID和服务名
		 * @param treeItemID 弹出窗口中左边树形结构的数据来源,可以是基础资料ID和服务名
		 * @param treeRootValue 树的根节点ID值
		 * @param treeLabelField 树形结构节点文本的字段名
		 * @param treeIDField 树形结构节点的ID字段名
		 * @param treepidField 树形数据源中节点指向父节点的字段名
		 * @param itemParentID 表格数据源中与树形数据源id对应的字段名
		 * @param multiSelected 是否可以多选
		 * @param callback 点“确定”后的回调函数
		 * 
		 */		
		public function EDIBasicDataWindow(itemID:String,treeItemID:String="",treeRootValue:String="0",treeLabelField:String="",treeIDField:String="",treepidField:String="",itemParentID:String="",multiSelected:Boolean=false,callback:Function=null,datasouName:String="tb_tree",logicDelete:Boolean = false,paramMap:HashMap = null)
		{
			super();
			this.width=600;
			this.height=400;
			
			this.itemID=itemID;
			this.treeItemID=treeItemID;
			this.treeRootValue=treeRootValue;
			this.treeLabelField=treeLabelField;
			
			this.treeIDField=treeIDField;
			this.treepidField=treepidField;
			this.itemParentID=itemParentID;
			this.multiSelected=multiSelected;
			this.callback=callback;
			this.datasouName=datasouName;
			this.logicDelete = logicDelete;
			this.hasShowAlert = false;
			this.paramMap = paramMap;
			
			setStyle("fontSize","12");
			MainBox=new VBox();
			MainBox.setStyle("horizontalAlign","right");
			MainBox.percentHeight=100;
			MainBox.percentWidth=100;
			addChild(MainBox);

			CheckAll=new CheckBox();
			CheckAll.label="显示全部下级";
			CheckAll.selected=false;
			CheckAll.addEventListener(MouseEvent.CLICK,checkAllHandler);
			MainBox.addChild(CheckAll);
			
			HDivBox=new HDividedBox();
			HDivBox.percentHeight=100;
			HDivBox.percentWidth=100;
			
			LeftBox=new VDividedBox();
			LeftBox.percentHeight=100;
			LeftBox.percentWidth=35;
			
			RightBox=new Box;
			RightBox.percentHeight=100;
			RightBox.percentWidth=65;
			
//			if(treeItemID!="" && treeItemID !=null)
			HDivBox.addChild(LeftBox);
				
			HDivBox.addChild(RightBox);
			
			ItemTree=new EDITree();
			ItemTree.showRoot=true;
			ItemTree.percentHeight=60;
			ItemTree.percentWidth=100;
//			ItemTree.addEventListener(MouseEvent.CLICK,itemClassSelectHandler);
			LeftBox.addChild(ItemTree);
			
			ItemList=new List();
			ItemList.percentHeight=90;
			ItemList.percentWidth=100;
			//build by HW
			LeftBottomHbox = new VBox();
			LeftBottomHbox.percentWidth = 100;
			LeftBottomHbox.percentHeight = 40;
			LeftBottomHbox.addChild(ItemList);
			BtnBox = new HBox();
			BtnBox.percentWidth = 100;
			BtnBox.percentHeight = 10;
			clearBtn = new Button();
			clearBtn.label = "清除";
			delBtn = new Button();
			delBtn.label = "删除";
			BtnBox.addChild(clearBtn);
			BtnBox.addChild(delBtn);
			LeftBottomHbox.addChild(BtnBox);
			
			LeftBox.addChild(LeftBottomHbox);
			//LeftBox.addChild(ItemList);
			
			ItemList.dataProvider=returnArr;
			ItemList.labelField="caption";
			ItemList.allowMultipleSelection=this.multiSelected;
			//添加删除按钮,故而取消该List时间监听 Build By HW
			delBtn.addEventListener(MouseEvent.CLICK,delBtnSelectedHandler);
			this.addEventListener(EDIBasicDataEvent.Data_Delete_Render,delItemRender);
			clearBtn.addEventListener(MouseEvent.CLICK,clearBtnHandler);
			//ItemList.addEventListener(ListEvent.ITEM_CLICK,listClickHandler);
			EDIApplication.outputDebug("tree DataSourceName:  "+datasouName);
			ItemTree.dataSourceName=datasouName;
			ItemTree.labelField="@"+treeLabelField;//"@caption";
			ItemTree.idField=treeIDField;//"id";
			ItemTree.dataField=treeLabelField;//"caption";
			ItemTree.pidField=treepidField;//"parentid";
			ItemTree.rootValue=treeRootValue;//"0";
			
			proxy=new HttpProxy(null,true,this.ds);
			proxy.url=EDIApplication.path+EDIApplication.appName+".app";
			if(treeItemID!="" && treeItemID!=null)
			{
				if(treeItemID.indexOf("Services")==0)
				{
					EDIApplication.outputDebug("paramMap的长度为: "+this.paramMap.size());
					if(null==this.paramMap || this.paramMap.size()<=0){//如果参数为空
						var arr:Array=new Array();
						var defaultUnitPara:Array=new Array();
						var para1:Object=new Object();
						para1["name"]="unitId";
						para1["value"]="";
						arr.push(para1);
					
						var para2:Object=new Object();
						para2["name"]="showUnder";
						para2["value"]="true";
						arr.push(para2);
						//ServerProxy.getWebInfo("tb_tree","Services.serviceGetServerUnits","",arr,getEqlDataHandeler);
						ServerProxy.getWebInfo(datasouName,this.treeItemID,"",arr,getEqlDataHandeler);
					
						//获取默认的部门
//						var para:Object=new Object();
//						para["name"]="ediSessionId";
//						para["value"]=EDIApplication.sessionId;
//						defaultUnitPara.push(para);
						ServerProxy.getWebInfo("units","Services.serviceGetUserUnits","",defaultUnitPara,getUserUnitsHandeler);
					}else{
						EDIApplication.outputDebug("进入else语句.");
						var arr:Array=new Array();
						var keysSet:Array = this.paramMap.keySet();
						EDIApplication.outputDebug("paramMap的长度为2: "+paramMap.size());
						for(var i:int=0;i<keysSet.length;i++){
							var obj:Object=new Object();
							obj["name"] = keysSet[i];
							obj["value"] = this.paramMap.find(keysSet[i]);
							arr.push(obj);
						}
						ServerProxy.getWebInfo(datasouName,this.treeItemID,"",arr,getEqlDataHandeler);
					}
				}
				else
				{
//					+" where isdel=0"
					eql="select "+treeLabelField+","+treeIDField+","+treepidField+" from "+treeItemID;
					if(logicDelete){
						eql = eql+"  where isdel=0";
					}
					var treePara:EPOParam=new EPOParam();
					treePara.EQL =eql;
					//TODO下面这句需要重写
					treePara.dsName = datasouName;
					proxy.addPoParam(treePara);
					proxy.send();
					proxy.resultCallback=getEqlDataHandeler;
				}
			}

			ItemGrid=new EDIDataGrid();
			ItemGrid.percentHeight=100;
			ItemGrid.percentWidth=100;
			ItemGrid.editable=false;
			ItemGrid.doubleClickEnabled=true;
			ItemGrid.haveButtonBar=false;
			ItemGrid.haveSumLine=false;
//			ItemGrid.addEventListener(MouseEvent.DOUBLE_CLICK,doubleClickHandler);
			RightBox.addChild(ItemGrid);
			MainBox.addChild(HDivBox);
			
			ResultTxt=new TextInput();
			ResultTxt.percentWidth=100;
			MainBox.addChild(ResultTxt);
			//隐藏用户调试信息框
			ResultTxt.visible = false;
			var okBox:HBox = new HBox();
			okBox.percentWidth = 100;
			var myLabel:Label = new Label();
			myLabel.text = "";
			myLabel.percentWidth = 45;
			okBox.addChild(myLabel);
			OkBtn=new Button();
			OkBtn.label="确定";
			//OkBtn.horizontalGradientMatrix(
			okBox.addChild(OkBtn);
			MainBox.addChild(okBox);
		
			OkBtn.addEventListener(MouseEvent.CLICK,OkBtnHandler);
			//ItemGrid.addEventListener(ListEvent.ITEM_CLICK,empGridEditHandler);
			ItemGrid.addEventListener(EDIBasicDataEvent.Data_Delete,empGridEditDeleteHandler);
			ItemGrid.addEventListener(EDIBasicDataEvent.Data_Insert,empGridEditInsertHandler);
			this.addEventListener(FlexEvent.CREATION_COMPLETE,createHandler);
		}
		private function createHandler(event:FlexEvent):void
		{
			var i:int;
			var colXml:XML;
			var column:DataGridColumn;
			var fieldsStr:String="";
			var tempStr:String="";
			
			ItemGrid.dataSourceName="tb_itemgrid";
			eql=" from "+itemID +" ";
			
			column=new CheckBoxColumn();
			column.width=50;
			//column.fieldType="选择";
			column.sortable=false;
			column.editable=false;
			//column.headerText="";
			column.dataField="selected";
			//column.table=ItemGrid.dataSourceName;
			//if(this.multiSelected){
			if(multiSelected)
				column.headerRenderer=new ClassFactory(CheckBoxHeaderRenderer);
			else
				column.headerRenderer = new ClassFactory(CheckBoxHeaderRendererForSingle);
			//}
			//column.=false;
			column.itemRenderer=new ClassFactory(CheckBoxItemRenderer);;
			ItemGrid.columns=ItemGrid.columns.concat(column);
			
			if(this.itemID.indexOf("Services")==0)
			{
				column=new EDIDataGridColumn();
				column.headerText="id";
				column.dataField="id";
				column.width=0;
				ItemGrid.columns=ItemGrid.columns.concat(column);
				
				column=new EDIDataGridColumn();
				column.headerText="名称";
				column.dataField="caption";
				ItemGrid.columns=ItemGrid.columns.concat(column);
			}
			else
			{
				column=new EDIDataGridColumn();
				column.headerText="id";
				column.dataField="fid";
				column.width=0;
				ItemGrid.columns=ItemGrid.columns.concat(column);
				
				column=new EDIDataGridColumn();
				column.headerText="编号";
				column.dataField="fcode";
				ItemGrid.columns=ItemGrid.columns.concat(column);
				
				column=new EDIDataGridColumn();
				column.headerText="名称";
				column.dataField="fname";
				ItemGrid.columns=ItemGrid.columns.concat(column);
				
				column=new EDIDataGridColumn();
				column.headerText="序号";
				column.dataField="findex";
				ItemGrid.columns=ItemGrid.columns.concat(column);
				
				column=new EDIDataGridColumn();
				column.headerText="描述";
				column.dataField="fdescription";
				ItemGrid.columns=ItemGrid.columns.concat(column);
			}

			//ds.getDataTable("tb_tree").onDataRollingAfter=dataRoolingHandler;
			if(!this.treeItemID)
			{
				ItemTree.dataProvider=new XMLList(<><row idValue="0" id="0" parentid="0" caption="全部资料"></row></>);
				refreshGrid();
			}
			ItemTree.addEventListener(ListEvent.ITEM_CLICK,treeItemClickHandler);
			CheckAll.addEventListener(Event.CHANGE,checkAllHandler);
			//ItemTree.dataProvider=this.ds.getDataTable("tb_base").toTree("id","parentid","0","caption");
		}
		private function getUserUnitsHandeler(event:HttpProxyResult):void
		{
			var resultXml:XML;
			var unitId:String;
			var para1:Object;
			var para2:Object;
			var arr:Array;
			while(event.poNext())
			{
				resultXml=new XML(event.currentPoResult.dataSource)
				try
				{
					if(XMLList(resultXml.table[0].row).length()>1)
					{
						unitId=XMLList(resultXml.table[0].row)[0]["id"];

						arr=new Array();
						para1=new Object();
						para1["name"]="unitId";
						para1["value"]=unitId;
						arr.push(para1);
						
						if(CheckAll.selected)
						{
							para2=new Object();
							para2["name"]="showUnder";
							para2["value"]="true";
							arr.push(para2);
						}
						else
						{
							para2=new Object();
							para2["name"]="showUnder";
							para2["value"]="false";
							arr.push(para2);
						}
						///build by HW for  at 2010-06-07 18:23
						// 初始化的时候,右侧的datagrid不取任何值,点击左侧树架构,才根据树节点ID,取的右侧datagrid的值
						//ServerProxy.getWebInfo("tb_itemgrid","Services.serviceGetUnitUsers","",arr,getEqlDataHandeler); 
						//原来的代码////ServerProxy.getWebInfo("tb_itemgrid","Services.serviceGetUnitUsers","",arr,getEqlDataHandeler);
					}
				}
				catch(e:Error)
				{
					
				}
			}
		}
		
		/**
		 * 基础资料窗口弹出方法，同时把returnArr中已有的内容显示在界面上
		 * 
		 */		
		override public function showModel():void
		{
			super.showModel();
			//this.CheckAll.selected = false;
			var xmlcoll:XMLListCollection;
			var i:int;
			var xmlList:XMLList;
			//如果需要按照树状进行选择，则需要把已经选择的项目显示为空
			if(this.treeItemID)
			{
				ItemGrid.dataProvider=null;
			}
			
			if(ItemGrid.dataProvider)
			{
				//xmlcoll=XMLListCollection(ItemGrid.dataProvider);
				xmlList=XMLList(ItemGrid.dataProvider).copy();
				xmlcoll=new XMLListCollection(xmlList);
				for(i=0;i<xmlcoll.length;i++)
				{
					xmlcoll[i].selected=false;
				}
			}
			this.ResultTxt.text="";
			for(i=0;i<this.returnArr.length;i++)
			{
				this.ResultTxt.text=this.ResultTxt.text+returnArr[i].caption+";";
			}
		}
		/**
		 * 从服务器段取得表格数据处理方法
		 * @param event
		 * 
		 */		
		private function getEqlDataHandeler(event:HttpProxyResult):void
		{
			var i:int;
			var column:DataGridColumn;
			var columnNum:int=1;
			var rowXml:XML;
			var fieldsStr:String="";
			var field:String="";
			var tempStr:String="";
			var sourcetable:String="";
			var resultXml:XML;
			while(event.poNext())
			{
//				EDIApplication.outputDebug("EDIBasicDataWindow-->getEqlDataHandeler-查询数据库赋值: "+event.currentPoResult.dataSource);
				this.ds.addDataTable(event.currentPoResult.dataSource);
			}
		} 
		/**
		 * 设置哪些列需要显示，可以指定多个字段
		 * @param col
		 * 
		 */		
		public function setVisibleField(col:String):void
		{
			//调用该函数指定显示的列
			visibleColObj[col]=col;
			visibleColNum=visibleColNum+1;
		}
		/**
		 * 设置选择后设置在EDIBasicData文本框中显示的字段名，只能指定一个字段
		 * @param val
		 * 
		 */		
		public function setReturnField(val:String):void
		{
			this.displayField=val;
		}
		
		private function treeItemClickHandler(event:ListEvent):void
		{
			refreshGrid();
		}
		private function refreshGrid():void
		{
			var i:int;
			var fliter:String="";
			var idField:String=treeIDField;
			var rows:XMLList;
			var xml:XML;
			var para1:Object;
			var para2:Object;
			EDIApplication.outputDebug("ItemTree: "+ItemTree.dataProvider);
			if (itemID.indexOf("Services")==0)
			{
				var arr:Array=new Array();
				para1=new Object();
				para1["name"]="unitId";
				para1["value"]=ItemTree.selectedItem["@idValue"];
				arr.push(para1);
				
				if(CheckAll.selected)
				{
					para2=new Object();
					para2["name"]="showUnder";
					para2["value"]="true";
					arr.push(para2);
				}
				else
				{
					para2=new Object();
					para2["name"]="showUnder";
					para2["value"]="false";
					arr.push(para2);
				}
				//ServerProxy.getWebInfo("tb_itemgrid","Services.serviceGetUnitUsers","",arr,getEqlDataHandeler); 
				ServerProxy.getWebInfo("tb_itemgrid",this.itemID,"",arr,getEqlDataHandeler);
			}
			else
			{
				eql="select fid,fname,fcode,findex ,fdescription from  "+this.itemID ;
				if(this.treeItemID)
				{
					if(ItemTree.selectedItem)
					{
						if(CheckAll.selected)
						{
							xml=XML(ds.getDataTable(datasouName).tree.treeNodeChildQItem(ItemTree.selectedItem));
							if(xml)
							{
								rows=xml..row;
								for(i=0;i<rows.length();i++)
								{
									if(fliter.length>0)
										fliter=fliter+",'"+rows[i][idField]+"'";
									else
										fliter="'"+rows[i][idField]+"'";
								}
							}
						}
						else
						{
							fliter="'"+ItemTree.selectedItem["@idValue"]+"'";
						}
					}
//					and isdel=0
					eql=eql+" where "+ itemParentID +" in ("+fliter+") ";
					if(logicDelete){
						eql = eql+" and isdel=0";
					}
				}

				//mx.controls.Alert.show(eql+" where "+ itemID+"."+itemParentID +" in ("+fliter+")");
				var desPara:EPOParam=new EPOParam();
				desPara.EQL =eql;
				//TODO下面这句需要重写
				desPara.dsName = "tb_itemgrid";
				proxy.addPoParam(desPara);
				proxy.resultCallback=getEqlDataHandeler;
				proxy.send();
			}
//			Alert.show("数据明细："+eql);
		}
			
		private function OkBtnHandler(event:MouseEvent):void
		{
			var item:Object;
			//CheckAll.selected = false;
			hasShowAlert = false;
			
			if(this.ediBasicData)
			{
				//mx.controls.Alert.show("给基础资料赋值:"+this.ResultTxt.text.substring(0,this.ResultTxt.text.indexOf(";")-1));
//				ediBasicData.ediValue=this.ResultTxt.text.substring(0,this.ResultTxt.text.indexOf(";"));
				//给ediBasicData复制值
				ediBasicData.returnArr.removeAll();
				EDIApplication.outputDebug("111111:  "+ediBasicData.returnArr.length.toString());
				for each(var obj:Object in returnArr)
				{
					item=new Object();
					for(var str:String in obj)
					{
						item[str]=obj[str];
					}
					ediBasicData.returnArr.addItem(item);
				}
			}
			if(this.returnTxt)
			{
				this.returnTxt.text=this.ResultTxt.text;
			}
			if(callback!=null)
			{
				callback.call();
			}
			hasShowAlert = false;
			this.close(); 
		}

		private function empGridEditDeleteHandler(event:EDIBasicDataEvent):void
		{
			var str:String;
			str=event.value[displayField]+";";
			delItem(str,event.value[saveField]);
			returnArr.refresh();
			//Alert.show(displayField+":"+XML(event.value).toXMLString()+":"+XML(event.value)[displayField] );
//			for(var s:String in event.value)
//			{
//				Alert.show(s);
//			}
		}
		private function delItem(str:String,idStr:String):void
		{
			var i:int; 
			var tempStr:String;
			if(ResultTxt.text.indexOf(str)>=0)
			{
				ResultTxt.text=ResultTxt.text.replace(str,"");
			}
			for(i=0;i<returnArr.length;i++)
			{
				tempStr=returnArr[i]["id"];
				///Alert.show(tempStr+":"+str);
				if(tempStr==idStr)
				{
					
					var delEvent:EDIBasicDataEvent = new EDIBasicDataEvent(EDIBasicDataEvent.Data_Delete_Render,true);
					var delObj:Object = new Object();
					EDIApplication.outputDebug("id: "+returnArr[i]["id"]);
					delObj["id"] = returnArr[i]["id"];
					delObj["caption"] = returnArr[i]["caption"];
					delObj["selected"] = returnArr[i]["selected"];
					delEvent.value = delObj;
					this.dispatchEvent(delEvent);
					returnArr.removeItemAt(i);
					returnArr.refresh();
				}
			}
		}
		private function empGridEditInsertHandler(event:EDIBasicDataEvent):void
		{
			var str:String;
			var obj:Object=new Object();
			if(returnArr.length==0 || this.multiSelected)
			{
				str=event.value[displayField]+";";
//				EDIApplication.outputDebug("str: "+str);
//				EDIApplication.outputDebug("event.value[\"id\"].toString(): "+event.value[saveField].toString());
//				
//				EDIApplication.outputDebug("saveField: "+saveField);
//				EDIApplication.outputDebug("displayField: "+displayField);
				if(!findInArrayCollecton(returnArr,"id",event.value[saveField].toString()))
				{
					obj["caption"]=event.value[displayField].toString();
					obj["id"]=event.value[saveField].toString();
					returnArr.addItem(obj);
					returnArr.refresh();
				}
				if(ResultTxt.text.indexOf(str)<0)
				{
					ResultTxt.text=ResultTxt.text+str;
				}
			}
			else
			{
				if(!this.hasShowAlert){
					Alert.yesLabel = "确定";
					EDIAlert.show("当前设置为单选！","温馨提示",Alert.YES,null,multiSelectOk,null,Alert.YES);
					var ev:EDIBasicDataEvent = new EDIBasicDataEvent(EDIBasicDataEvent.Data_Delete_Render);
					ev.value = event.value;
					ItemGrid.dispatchEvent(ev);
					this.hasShowAlert = true;
				}
			}
		}
		/****
		 * 单选时,弹出提示框的回调函数
		 */
		private function multiSelectOk(e:Object):void{
			this.hasShowAlert = false;
		}
		private function findInArrayCollecton(arr:ArrayCollection,key:String,value:String):Boolean
		{
			var flag:Boolean=false;
			for each(var obj:Object in arr)
			{
//				EDIApplication.outputDebug("value: "+value);
//				EDIApplication.outputDebug("[key]: "+key);
//				EDIApplication.outputDebug("obj: "+obj.toString());
//				EDIApplication.outputDebug("obj[key]: "+obj[key].toString());
				if(obj[key].toString()==value)
				{
					flag=true;
				}
			}
//			EDIApplication.outputDebug("flag: "+flag);
			return flag;
		}

		private function checkAllHandler(event:Event):void
		{
			var evt:ListEvent;
			if(ItemTree.selectedItem)
			{
				evt=new ListEvent("itemDoubleClick");
				treeItemClickHandler(evt);
			}
		}
		private function listClickHandler(event:ListEvent):void
		{
			var contextMenu:ContextMenu = new ContextMenu();
			var contextMenuItem:ContextMenuItem;
			contextMenuItem=new ContextMenuItem("删除");
			contextMenuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,delSelectedHandler);
			contextMenu.customItems.push(contextMenuItem);
			this.contextMenu = contextMenu; 
			this.contextMenu.hideBuiltInItems();
		}
		private function delSelectedHandler(event:ContextMenuEvent):void
		{
			//trace("基础资料-->删除的长度为: "+ItemList.selectedItems.length);
			var delArray:Array = ItemList.selectedItems;
			for(var j:int=0;j<delArray.length;j++){
				//trace("基础资料-->删除的内容为: "+delArray[j]["caption"]);
				if(delArray[j])
				delItem(delArray[j]["caption"]+";",delArray[j]["id"]);
			}
				
			
		}
		
		/****
		 * 删除按钮的部分处理
		 * 
		 */
		private function delBtnSelectedHandler(event:MouseEvent):void
		{
			//trace("基础资料-->删除的长度为: "+ItemList.selectedItems.length);
			var delArray:Array = ItemList.selectedItems;
			for(var j:int=0;j<delArray.length;j++){
				//trace("基础资料-->删除的内容为: "+delArray[j]["caption"]);
				if(delArray[j])
				delItem(delArray[j]["caption"]+";",delArray[j]["id"]);
			}
				
			
		}
		
		private function clearBtnHandler(event:MouseEvent):void{
			var i:int;
			var tempStr:String;
//			if(ResultTxt.text.indexOf(str)>=0)
//			{
//				ResultTxt.text=ResultTxt.text.replace(str,"");
//			}
//			for(i=0;i<returnArr.length;i++)
//			{
//				tempStr=returnArr[i]["caption"];
//				///Alert.show(tempStr+":"+str);
////				if(tempStr+";"==str)
////				{
//					returnArr.removeItemAt(i);
//					
////				}
//			}
			ediBasicData.returnArr.removeAll();
			returnArr.removeAll();
			this.ResultTxt.text = "";
			EDIApplication.outputDebug("222222:  "+ediBasicData.returnArr.length.toString());
			EDIApplication.outputDebug("333333:  "+returnArr.length.toString());
			returnArr.refresh();
		}
		/**
		 * 设置已选择基础资料的数据
		 * @param arr
		 * 
		 */		
		public function set returnArr(arr:ArrayCollection):void
		{
			var item:Object;
			this._returnArr.removeAll();
			for each(var obj:Object in arr)
			{
				item=new Object();
				for(var str:String in obj)
				{
					item[str]=obj[str];
				}
				this._returnArr.addItem(item);
				ResultTxt.text=ResultTxt.text+obj["caption"]+";";
			}
			this.ItemList.dataProvider=this._returnArr;
		}
		/**
		 * 取得已选择基础资料的数据
		 * @return 
		 * 
		 */		
		public function get returnArr():ArrayCollection
		{
			return _returnArr;
		}
		
		private function delItemRender(event:EDIBasicDataEvent):void{
			EDIApplication.outputDebug("EDIBasicDataWindow-->delItemRender");
		}
	}
}